﻿using OccurrencesCore.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OccurrencesCore.Infrastructure
{
    public static class UnitOfWork
    {
        private const string UNIT_OF_WORK = "OCCURRENCES_UNIT_OF_WORK";

        public static void CreateEntityFrameworkContext()
        {
        }

        public static ModelContainer Context
        {
            get
            {
                if (!HttpContext.Current.Items.Contains(UNIT_OF_WORK))
                {
                    HttpContext.Current.Items.Add(UNIT_OF_WORK, new ModelContainer());
                }

                return (ModelContainer)HttpContext.Current.Items[UNIT_OF_WORK];
            }
        }

        public static void CommitEntityFrameworkContext()
        {
            try
            {
                Context.SaveChanges();
            }
            catch (Exception)
            {
            }
        }

        public static void DisposeEntityFrameworkContext()
        {
            Context.Dispose();
            HttpContext.Current.Items.Remove(UNIT_OF_WORK);
        }
    }
}
